package com.own.business.system.user.business.core;

import com.own.business.login.core.hook.GetUserAccountTypeHook;
import com.own.business.system.user.business.service.SystemUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * GetUserAccountTypeHookImpl
 *
 * @author chenxueli
 * @date 2024-03-20 15:36:00
 */
@Slf4j
@Component
public class GetUserAccountTypeHookImpl implements GetUserAccountTypeHook {

    private static final Map<Long, Integer> USER_ACCOUNT_TYPE_MAP = new HashMap<>();
    @Resource
    private SystemUserService systemUserService;

    /**
     * 获取用户账号类型
     *
     * @param userId 用户id
     * @return 账号类型 1=普通账号 2=管理员
     */
    @Override
    public int get(Long userId) {
        if (USER_ACCOUNT_TYPE_MAP.containsKey(userId)) {
            return USER_ACCOUNT_TYPE_MAP.get(userId);
        }
        var accountType = systemUserService.getAccountTypeByUserId(userId);
        USER_ACCOUNT_TYPE_MAP.put(userId, accountType);
        return accountType;
    }

}
